define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/user/index',
                    add_url: 'user/user/add',
                    edit_url: 'user/user/edit',
                    // del_url: 'user/user/del',
                    multi_url: 'user/user/multi',
                    table: 'user',
                }
            });

            var table = $("#table");
            //在普通搜索渲染后
            table.on('post-common-search.bs.table', function (event, table) {
                var form = $("form", table.$commonsearch);
                $("input[name='levels.name']", form).addClass("selectpage").data("source", "user/level/selectpage").data("primaryKey", "name").data("field", "name").data("orderBy", "id desc");
                $("input[name='group.name']", form).addClass("selectpage").data("source", "user/group/selectpage").data("primaryKey", "name").data("field", "name").data("orderBy", "id desc");
                Form.events.cxselect(form);
                Form.events.selectpage(form);
            });
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'user.id',
                search: true,
                fixedColumns: true, // 固定列代码
    fixedRightNumber: 1, // 固定右侧第一列
    // fixedNumber:5, // 固定左侧10列
                columns: [
                    [
                        { checkbox: true },
                        { field: 'uuid', title: __('Id'), sortable: true },
                        { field: 'group_id', title: __('会员'), formatter: Table.api.formatter.label, searchList: Config.groupList },
                        // { field: 'level', title: __('星级'), formatter: Table.api.formatter.label, searchList: { '0': __('普通用户'), '1': __('经销商'), '2': __('主管'), '3': __('经理'), '4': __('高级经理'), '5': __('总监'), '6': __('总裁'), '7': __('董事') } },
                        { field: 'puser.nickname', title: __('推荐人昵称'), operate:'LIKE' },
                        { field: 'puser.mobile', title: __('推荐人手机号'), operate:'LIKE' },
                        { field: 'nickname', title: __('Nickname'), operate: 'LIKE' },
                        { field: 'mobile', title: __('Mobile'), operate: 'LIKE' },
                        { field: 'avatar', title: __('Avatar'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false },
                        { field: 'code', title: __('邀请码'), operate: false, sortable: true },
                        { field: 'push', title: __('直属人数'), operate: false },
                        { field: 'push_performance', title: __('直属业绩'), operate: false },
                        { field: 'team', title: __('团队人数'), operate: false},
                        { field: 'team_performance', title: __('团队业绩'), operate: false},
                        { field: 'options', title: __('期权'), operate: false, sortable: true },
                        { field: 'score', title: __('积分'), operate: false, sortable: true },
                        { field: 'money', title: __('可提现余额'), operate: false, sortable: true },
                        { field: 'no_money', title: __('不可提现余额'), operate: false, sortable: true },
                        { field: 'consumption', title: __('累计消费'), operate: false, sortable: true },
                        { field: 'frozen_money', title: __('冻结分红金额'), operate: false, sortable: true },
                        { field: 'total_dividend', title: __('应分红金额'), operate: false, sortable: true },
                        { field: 'use_dividend', title: __('未分红金额'), operate: false, sortable: true },
                        { field: 'y_dividend', title: __('已分红金额'), operate: false},
                        { field: 'total_interests', title: __('累计权益值'), operate: false, sortable: true },
                        { field: 'scan', title: __('扫码授权'), formatter: Table.api.formatter.status, searchList: { '1': __('是'), '0': __('否') } },
                        { field: 'status', title: __('Status'), formatter: Table.api.formatter.status, searchList: { normal: __('Normal'), hidden: __('Hidden') } },
                        {field: 'jointime', title: __('Jointime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {
                            field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'yue',
                                    title: '充值',
                                    text: '充值',
                                    refresh: true,
                                    classname: 'btn btn-primary btn-xs btn-view btn-dialog',

                                    url: 'user/user/yue',//接口地址 控制器名/方法名
                                },
                                {
                                    name: 'scan',
                                    title: '扫码授权',
                                    text: '扫码授权',
                                    refresh: true,
                                    classname: 'btn btn-primary btn-xs btn-view btn-ajax',
                                    // confirm: "{scan}" == 1 ? '是否取消授权？' : '是否授权扫码？',
                                    confirm: function (row) {
                                        return row.scan == 1 ? '是否取消授权？' : '是否授权扫码？'
                                    },
                                    url: 'user/user/scan',//接口地址 控制器名/方法名
                                }
                            ]

                        }
                    ]
                ]

            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        scan: function () {
            Controller.api.bindevent();
        },
        yue: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});